package com.t235.gc.gateway;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.t235.gc.AjaxResult;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class DemoController {
    @Resource
    private RestTempUtils restTempUtils;

    @GetMapping("/serverUrls")
    public List<String> serverUrls() {
        return Arrays.asList("http://localhost:8001",
                "http://localhost:8002",
                "http://localhost:8003");
    }

    @RequestMapping("/hello")
    public AjaxResult hello(String port) {
        String url = "http://localhost:" + port + "/hello";
        return restTempUtils.getForObject(url, AjaxResult.class);
    }

    @RequestMapping("/8001me")
    public Object me() {
        return restTempUtils.postForObject("http://localhost:8001/me", Object.class);
    }

    @RequestMapping("/8001Post")
    public Object post() {
        MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>();
        multiValueMap.add("name", "张三");
        multiValueMap.add("password", "123456");
        return restTempUtils.postForObject("http://localhost:8001/post", multiValueMap, AjaxResult.class);
    }

    @RequestMapping("/8001Add")
    public Object add() throws JsonProcessingException {
        Map<String, Object> params = new HashMap<>();
        params.put("userid", 1000L);
        params.put("nickname", "T235班");
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonString = objectMapper.writeValueAsString(params);
        return restTempUtils.postForObject("http://localhost:8001/add", jsonString, AjaxResult.class);
    }
}
